package com.sikao.app.ui;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.sikao.app.R;
import com.sikao.app.adapter.GvAddStepPricesTypeProfilePicItemAdapter;
import com.sikao.app.bean.GetImageInfoBean;
import com.sikao.app.bean.GetImageInfoSubmitBean;
import com.sikao.app.bean.UploadPicturesBean;
import com.sikao.app.dao.GetImageInfoModel;
import com.sikao.app.dao.UploadPicturesModel;
import com.sikao.app.myinterface.Callback;
import com.sikao.app.net.HttpApi;
import com.sikao.app.net.OnNetBackListener;
import com.sikao.app.ui.base.BaseActivity;
import com.sikao.app.utils.JSONUtil;
import com.sikao.app.utils.Utils;
import com.sikao.app.view.MyGridView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Sun on 2016/8/15.
 * 编辑仓单照片
 */
public class ReleaseGoodsStorePicActivity extends BaseActivity implements View.OnClickListener {

	private TextView titleTv;
	private TextView title_right_tv;
	private ImageView backIv;
	private MyGridView gv_release_goods_store_pic;
	private UploadPicturesModel uploadPicturesModel;
	GvAddStepPricesTypeProfilePicItemAdapter gvAddStepPricesTypeProfilePicItemAdapter;//图样适配器(复用图样的)
	private List<String> picList = new ArrayList<String>();//仓单图片
	int picIndex = 0;//点击的图片的下标
	String storePic = "";
	private int releaseGoodsState = 1;//如果是商品详情进入的3 则进去后不能编辑
	public static final int REQUEST_GET_PHOTO = 1001;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.activity_release_goods_store_pic);
		super.onCreate(savedInstanceState);
	}

	@Override
	protected void initData() {
		Intent intent = getIntent();
		storePic = intent.getStringExtra("storePic");
		releaseGoodsState = intent.getIntExtra("releaseGoodsState", 1);//如果是商品详情进入的3 则进去后不能编辑

		Utils.showLog("=======仓单 storePic releaseGoodsState：" + releaseGoodsState + "  ==3" + (3 == releaseGoodsState));
		if (null == storePic || "".equals(storePic)) {
			storePic = "";
			picList.add("");//刚进去默认有一张图片
			Utils.showLog("=======仓单初始进入storePic：" + storePic);
		} else {
			//说明之前保存过 从网络中获取图片地址

			picList.clear();
			//从网络获取 给picList赋值，同时添加一个空值
			getImageInfoFromIn();


		}

	}

	private void getImageInfoFromIn() {
		GetImageInfoSubmitBean getImageInfoSubmitBean = new GetImageInfoSubmitBean();
		getImageInfoSubmitBean.setIds(storePic);
		GetImageInfoModel getImageInfoModel = new GetImageInfoModel(this, new OnNetBackListener<GetImageInfoBean>() {
			@Override
			public void netBack(GetImageInfoBean data) {
				if (data == null) {
					Toast.makeText(ReleaseGoodsStorePicActivity.this, data.getMsg(), Toast.LENGTH_SHORT).show();
				} else {
					if (data.getStatus() == 1) {
						for (GetImageInfoBean.ResBean resBean : data.getRes()) {
							picList.add(HttpApi.BANNERUTL + resBean.getUrl());
						}
						picList.add("");
						gvAddStepPricesTypeProfilePicItemAdapter.notifyDataSetChanged();
					} else {
						Toast.makeText(ReleaseGoodsStorePicActivity.this, data.getMsg(), Toast.LENGTH_SHORT).show();
					}
				}
			}
		});
		getImageInfoModel.request(JSONUtil.toJSON(getImageInfoSubmitBean), true);

	}

	@Override
	protected void initView() {
		titleTv = (TextView) findViewById(R.id.title_middle_tv);
		title_right_tv = (TextView) findViewById(R.id.title_right_tv);


		backIv = (ImageView) findViewById(R.id.title_left_img);
		gv_release_goods_store_pic = (MyGridView) findViewById(R.id.gv_release_goods_store_pic);

	}

	@Override
	protected void initListener() {
		title_right_tv.setOnClickListener(this);
		backIv.setOnClickListener(this);
		title_right_tv.setOnClickListener(this);

	}

	@Override
	protected void init() {
		titleTv.setText("仓单图片");
		backIv.setVisibility(View.VISIBLE);
		title_right_tv.setVisibility(View.VISIBLE);
		title_right_tv.setText("确定");


		gvAddStepPricesTypeProfilePicItemAdapter = new GvAddStepPricesTypeProfilePicItemAdapter(this, picList, new Callback() {
			@Override
			public void notice(int position) {
				if (3 != releaseGoodsState) {//如果第一步已经保存 则仓单照片不能再编辑
					picIndex = position;
					if (position == (picList.size() - 1)) {//第一次点击或者点的是最后一个
						Intent intent = new Intent(ReleaseGoodsStorePicActivity.this, SelectPictureActivity.class);
						startActivityForResult(intent, REQUEST_GET_PHOTO);
						overridePendingTransition(R.anim.bottom_push_in, R.anim.bottom_push_out);

					} else {
						createDialog(position);
					}
				}
			}
			// }
		});


		gv_release_goods_store_pic.setAdapter(gvAddStepPricesTypeProfilePicItemAdapter);


	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (resultCode == RESULT_OK) {
			switch (requestCode) {
				case REQUEST_GET_PHOTO:
					String picturePath = data.getStringExtra("path");
					picList.set(picIndex, picturePath);
					//上传图片
					uploadPicture(picIndex + ".png", picturePath);
					//添加一个新的空的照片
					picList.add("");
					gvAddStepPricesTypeProfilePicItemAdapter.notifyDataSetChanged();
					break;
				default:
					break;
			}
		}
	}

	private void uploadPicture(String type, String picturePath) {
		if (uploadPicturesModel == null) {
			uploadPicturesModel = new UploadPicturesModel(this, new OnNetBackListener<UploadPicturesBean>() {
				@Override
				public void netBack(UploadPicturesBean data) {
					if (data == null) {
						Toast.makeText(ReleaseGoodsStorePicActivity.this, "上传失败", Toast.LENGTH_SHORT).show();
						return;
					}

					if (data.getStatus().equals("1")) {
						String id = data.getRes().getArr().get(0).getId();
						if (TextUtils.isEmpty(storePic)) {
							storePic = id;
						} else {
							storePic = storePic + "," + id;
						}
						Utils.showLog("=======仓单添加后storePic：" + storePic);
					} else {
						Toast.makeText(ReleaseGoodsStorePicActivity.this, data.getMsg(), Toast.LENGTH_SHORT).show();
					}
				}
			});
		}
		Map<String, String> map = new HashMap<>();
		map.put(type, picturePath);
		uploadPicturesModel.request(map, true);
	}

	private void createDialog(final int position) {
		AlertDialog dialog;
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setMessage("确认删除吗?");
		builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
				picList.remove(position);
				//从storePic中去除
				if (TextUtils.isEmpty(storePic)) {
					storePic = "";
				} else {
					String[] sp = storePic.split(",");
					sp[position] = null;
					storePic = "";
					for (int i = 0; i < sp.length; i++) {
						if (sp[i] != null) {
							if ("".equals(storePic)) {
								storePic = sp[i];
							} else {
								storePic = storePic + "," + sp[i];
							}
						}
					}
				}
				//
				Utils.showLog("=======仓单删除后storePic：" + storePic);
				gvAddStepPricesTypeProfilePicItemAdapter.notifyDataSetChanged();

			}
		});
		builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
			}
		});
		dialog = builder.create();
		dialog.show();
	}


	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.title_left_img:
				back();
				break;
			case R.id.title_right_tv:
				back();
				break;
			default:
				break;
		}
	}

	@Override
	public void onBackPressed() {
		back();
	}

	private void back() {
		Intent intent = new Intent();
		intent.putExtra("storePic", storePic);
		if (TextUtils.isEmpty(storePic)) {
			intent.putExtra("isHaveStorePic", false);
		} else {
			intent.putExtra("isHaveStorePic", true);
		}
		setResult(ReleaseGoodsActivity.STORE_PIC_RESULT_CODE, intent);
		finish();
	}


}